/**
 * 
 */
package com.feizhi.common.upload.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.feizhi.common.OperateResult;
import com.feizhi.common.repo.Condition;
import com.feizhi.common.repo.GeneralRepo;
import com.feizhi.common.upload.bean.UploadedFile;

/**
 * @author shuaqiu 2013年11月29日
 * 
 */
public class UploadedFileService {

	private String table = "uploaded_file";

	private GeneralRepo repo;

	@Autowired
	public void setRepo(GeneralRepo repo) {
		this.repo = repo;
	}

	public OperateResult insert(List<UploadedFile> files) {
		return repo.batchInsert(table, files, "flag");
	}

	public List<UploadedFile> query(String refName, int refId) {
		Condition condition = buildCondition(refName, refId);

		List<UploadedFile> files = repo.query(table, condition,
				UploadedFile.class);
		return files;
	}

	/**
	 * @param refName
	 * @param refId
	 * @return
	 */
	private Condition buildCondition(String refType, int refId) {
		Condition condition = new Condition();
		condition.addEquals(refType, "refType");
		condition.addEquals(refId, "refId");
		return condition;
	}

	/**
	 * 删除实体的所有文件数据记录
	 * 
	 * @param refType
	 *            实体类型的名称
	 * @param refId
	 *            实体ID
	 * @return
	 */
	public OperateResult delete(String refType, int refId) {
		Condition condition = buildCondition(refType, refId);
		OperateResult result = repo.delete(table, condition);
		return result;
	}

	/**
	 * 删除指定的文件数据
	 * 
	 * @param id
	 * @return
	 */
	public OperateResult delete(int id) {
		OperateResult result = repo.delete(table, id);
		return result;
	}

	/**
	 * @param files
	 * @return
	 */
	public OperateResult delete(List<UploadedFile> files) {
		for (UploadedFile file : files) {
			repo.delete(table, file.getId());
		}
		return OperateResult.OK;
	}
}
